package com.example.demo.listener;

import com.example.demo.entity.CSDNAuthorEntity;
import com.example.demo.mapper.CSDNMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author haohulala
 * @version 1.0
 * @description: 消费作者信息
 * @date 2023/4/15 21:44
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "TOPIC_CSDN", selectorExpression = "AUTHOR", consumerGroup = "CSDN_AUTHOR")
public class AuthorListener implements RocketMQListener<CSDNAuthorEntity> {

    @Autowired
    private CSDNMapper csdnMapper;

    @Override

    public void onMessage(CSDNAuthorEntity csdnAuthorEntity) {
        if(csdnMapper.checkAuthor(csdnAuthorEntity)==0) {
            csdnMapper.insertOneAuthor(csdnAuthorEntity);
        }
        else {
            csdnMapper.updateOneAuthor(csdnAuthorEntity);
        }
        log.info("消费作者信息：{}", csdnAuthorEntity);
    }
}
